/*
 * Copyright (C) 2003, Velocityme Partnership
 * UserAccessSessionBean.java
 *
 * Created on 23 April 2003, 08:30
 */

package com.velocityme.session;

import com.velocityme.entity.ChangeDeltaItemTypeBean;
import com.velocityme.entity.ChangeDeltaTypeBean;
import com.velocityme.entity.PermissionBean;
import javax.ejb.*;
import javax.naming.*;
import java.util.*;
import com.velocityme.interfaces.*;
import com.velocityme.utility.InvalidKeyException;
import com.velocityme.utility.PermissionCache;
import com.velocityme.utility.PermissionDeniedException;
import java.rmi.RemoteException;
import javax.ejb.RemoveException;

/**
 *
 * @author  Robert Crida Work
 * @ejb.bean
 *           type="Stateless"
 *           cmp-version="2.x"
 *           name="UserAccessSession"
 *           jndi-name="ejb/UserAccessSession"
 *           view-type="local"
 *           transaction-type="Container"
 * @ejb.transaction type="Required"
 *
 * @ejb.util generate="physical"
 */
public class UserAccessSessionBean implements SessionBean {
    
    private SessionContext m_context;

    private SearchSessionLocal m_searchSessionLocal;

    private PermissionLocal m_permissionUserAccessCreate;
    private PermissionLocal m_permissionUserAccessDelete;
    /**
     * If the user has permission to create a new UserAccess in the specified 
     * node then go ahead and create it.
     *
     * @param p_key User's security key
     * @param p_node The node in which the user access will be created
     * @param p_value The UserAccess value object
     *
     * @ejb.interface-method view-type="local"
     **/
    public UserAccessLocal createUserAccess(com.velocityme.interfaces.KeySessionLocal p_keyLocal,
                                            com.velocityme.interfaces.NodeLocal p_nodeLocal,
                                            com.velocityme.interfaces.UserLocal p_userLocal,
                                            com.velocityme.interfaces.RoleLocal p_roleLocal)
        throws PermissionDeniedException, InvalidKeyException {
        try {
            if (m_searchSessionLocal.getAssignedPermissionsLocal(p_keyLocal, p_nodeLocal).contains(m_permissionUserAccessCreate)) {
                UserAccessLocal userAccessLocal = UserAccessUtil.getLocalHome().create(p_nodeLocal, p_userLocal, p_roleLocal);
                // clear the permission cache for this user
                PermissionCache.getInstance().clearUser(p_userLocal.getUserId());
                ChangeDeltaLocal changeDeltaLocal = ChangeDeltaUtil.getLocalHome().create(new Date(), ChangeDeltaTypeBean.CREATE_USER_ACCESS, p_keyLocal.getUserLocal(), p_userLocal.getNodeLocal().getDBObjectLocal());
                changeDeltaLocal.addItem(ChangeDeltaItemTypeBean.NODE, p_nodeLocal.getPathName());
                changeDeltaLocal.addItem(ChangeDeltaItemTypeBean.ROLE, p_roleLocal.getNodeLocal().getName());
                return userAccessLocal;
            }
            else
                throw new PermissionDeniedException();
        }
        catch(NamingException e) {
            throw new EJBException(e);
        }
        catch(CreateException e) {
            throw new EJBException(e);
        }
    }
    
    /**
     * If the user has permission to delete the user access then do so .
     *
     * @param p_key User's security key
     * @param p_userAccess The UserAccess object being deleted
     *
     * @ejb.interface-method view-type="local"
     **/
    public void deleteUserAccess(com.velocityme.interfaces.KeySessionLocal p_keyLocal, 
                                 com.velocityme.interfaces.UserAccessLocal p_userAccessLocal)
        throws PermissionDeniedException, InvalidKeyException {
        try {
            if (m_searchSessionLocal.getAssignedPermissionsLocal(p_keyLocal, p_userAccessLocal.getNodeLocal()).contains(m_permissionUserAccessDelete)) {
                ChangeDeltaLocal changeDeltaLocal = ChangeDeltaUtil.getLocalHome().create(new Date(), ChangeDeltaTypeBean.DELETE_USER_ACCESS, p_keyLocal.getUserLocal(), p_userAccessLocal.getUserLocal().getNodeLocal().getDBObjectLocal());
                changeDeltaLocal.addItem(ChangeDeltaItemTypeBean.NODE, p_userAccessLocal.getNodeLocal().getPathName());
                changeDeltaLocal.addItem(ChangeDeltaItemTypeBean.ROLE, p_userAccessLocal.getRoleLocal().getNodeLocal().getName());
                // clear the permission cache for this user
                PermissionCache.getInstance().clearUser(p_userAccessLocal.getUserLocal().getUserId());
                UserAccessUtil.getLocalHome().remove(p_userAccessLocal.getPrimaryKey());
            }
            else
                throw new PermissionDeniedException();
        }
        catch(RemoveException e) {
            throw new EJBException(e);
        }
        catch(NamingException e) {
            throw new EJBException(e);
        }
        catch(CreateException e) {
            throw new EJBException(e);
        }
    }
    
    /**
     * If the user has permission then get a list of accesses by user.
     *
     * @param p_keyLocal User's security key
     * @param p_userLocal The User whose access list is required
     *
     * @ejb.interface-method view-type="local"
     **/
    public java.util.Collection findByUserValue(com.velocityme.interfaces.KeySessionLocal p_keyLocal,
                                                com.velocityme.interfaces.UserLocal p_userLocal)
        throws InvalidKeyException {
        Collection values = new java.util.ArrayList();
        try {
            if (p_keyLocal.isValid()) {
                Collection userAccesses = UserAccessUtil.getLocalHome().findByUser(p_userLocal);
                Iterator i = userAccesses.iterator();
                while(i.hasNext()) {
                    UserAccessLocal userAccessLocal = (UserAccessLocal)i.next();
                    values.add(userAccessLocal.getUserAccessValue());
                }
            }
            else
                throw new InvalidKeyException();
        }
        catch(NamingException e) {
            throw new EJBException(e);
        }
        catch(FinderException e) {
            throw new EJBException(e);
        }
        return values;
    }
    
    /**
     * If the user has permission then get a list of accesses by node.
     *
     * @param p_key User's security key
     * @param p_node The Node whose access list is required
     *
     * @ejb.interface-method view-type="local"
     **/
    public java.util.Collection findByNodeValue(com.velocityme.interfaces.KeySessionLocal p_keyLocal,
                                                com.velocityme.interfaces.NodeLocal p_nodeLocal)
        throws InvalidKeyException {
        Collection values = new java.util.ArrayList();
        try {
            if (p_keyLocal.isValid()) {
                Collection userAccesses = UserAccessUtil.getLocalHome().findByNode(p_nodeLocal);
                Iterator i = userAccesses.iterator();
                while(i.hasNext()) {
                    UserAccessLocal userAccessLocal = (UserAccessLocal)i.next();
                    values.add(userAccessLocal.getUserAccessValue());
                }
            }
            else
                throw new InvalidKeyException();
        }
        catch(NamingException e) {
            throw new EJBException(e);
        }
        catch(FinderException e) {
            throw new EJBException(e);
        }
        return values;
    }
    
    /**
     * Create the Session Bean.
     * @throws CreateException 
     */
    public void ejbCreate() throws CreateException {
        try {
            m_searchSessionLocal = SearchSessionUtil.getLocalHome().create();
            m_permissionUserAccessCreate = PermissionUtil.getLocalHome().findByPrimaryKey(new PermissionPK(new Integer(PermissionBean.USER_ACCESS_CREATE)));
            m_permissionUserAccessDelete = PermissionUtil.getLocalHome().findByPrimaryKey(new PermissionPK(new Integer(PermissionBean.USER_ACCESS_DELETE)));
        }
        catch(NamingException e) {
            throw new CreateException(e.getMessage());
        }
        catch(FinderException e) {
            throw new CreateException(e.getMessage());
        }
    }

    public void ejbActivate() throws java.rmi.RemoteException {
    }
    
    public void ejbPassivate() throws java.rmi.RemoteException {
    }
    
    public void ejbRemove() throws java.rmi.RemoteException {
    }
    
    public void setSessionContext(javax.ejb.SessionContext sessionContext) throws java.rmi.RemoteException {
        m_context = sessionContext;
    }
    
}
